﻿''' <summary>
''' 發票統計報表
''' </summary>songrenhua 2009-05-21
''' <remarks></remarks>
Public Class Frm_ARSallinvoiceCountReports
    Implements IDServer.IDServer.IDServer
    Private WithEvents MainForm As Frm_MainForm
    Private ReportTxt As String = String.Empty
    Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp

    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property

#Region "主表單控制項"
    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd

    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel

    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete

    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit

    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave

    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function
#End Region


    Private Sub LodStatus()
        Try
            Dim SQL As String
            Dim DT As DataTable
            SQL = "SELECT top(1) '' LISTCODE,'' LISTA from BSC_OTHERDEFINEB union all "
            SQL += " SELECT LISTCODE,LISTA FROM DBO.BSC_OTHERDEFINEB WHERE ITEMNO IN  "
            SQL += " (SELECT ID AS ITEMNO FROM BSC_OTHERDEFINEA WHERE ITEMCODE = 'BILLSTATUSDEFINE')"
            DT = Exec(SQL, E_ExecType.DataTable)
            Me.CBStatus.DataSource = DT
            Me.CBStatus.DisplayMember = "LISTA"
            Me.CBStatus.ValueMember = "LISTCODE"
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub
    Private Sub LodType()
        Try
            Dim SQL As String
            Dim DT As DataTable
            SQL = " Select  Top(1) '' LISTCODE,'' LISTA from BSC_OTHERDEFINEB union all "
            SQL += " SELECT LISTCODE,LISTA FROM DBO.BSC_OTHERDEFINEB WHERE ITEMNO IN  "
            SQL += " (SELECT ID AS ITEMNO FROM BSC_OTHERDEFINEA WHERE ITEMCODE = 'InvoiceType')"
            DT = Exec(SQL, E_ExecType.DataTable)
            Me.CBType.DataSource = DT
            Me.CBType.DisplayMember = "LISTA"
            Me.CBType.ValueMember = "LISTCODE"
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub BtnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFind.Click
        SelectData(True)
    End Sub
    Private Sub SelectData(ByVal B As Boolean)
        Try

            Dim SQL As String = String.Empty
            Dim DT As DataTable
            Dim Status As String = Me.CBStatus.SelectedValue.ToString.Trim
            Dim Type As String = Me.CBType.SelectedValue.ToString.Trim
            Dim SDate As String = Me.DateTimePicker1.Value.ToString("yyyy-MM-dd")
            Dim EDate As String = Me.DateTimePicker2.Value.ToString("yyyy-MM-dd")
            If Me.RBShop.Checked = True Then
                SQL = " SELECT B.FSHORTNAME AS SHOP,COUNT(FNUM) AS QTY,SUM(A.FTAMOUNT)AS AMOUNT   FROM DBO.SAL_INVOICEA A       INNER JOIN SAL_SALEORDEA D ON A.FSALEORDENO=D.FSALEORDENO LEFT JOIN BSC_STOREHOUSE B ON A.SHOPNO = B. FCODE "
                If B = True Then
                    SQL += "  WHERE CONVERT(VARCHAR(10),FDATE,120) BETWEEN '" & SDate & "' AND '" & EDate & "' "
                    If Me.CBStatus.Text.ToString.Trim <> "" Then
                        SQL += " AND A.BillStatusid = '" & Status & "' "
                    End If
                    If Me.CBType.Text.ToString.Trim <> "" Then
                        SQL += " AND A.FTYPE = '" & Type & "' "
                    End If
                Else
                    SQL += " WHERE 1<>1 "
                End If
                SQL += "  GROUP BY B.FSHORTNAME    ORDER BY FSHORTNAME "
                DT = Exec(SQL, E_ExecType.DataTable)
                Me.DataGridView1.DataSource = DT
                BindDataGridViewCaption(Me.DataGridView1, Me.ReturnCaption(1))
            End If
            If Me.RBDate.Checked = True Then
                SQL = " SELECT CONVERT(VARCHAR(10),FDATE,120) FDATE ,COUNT(FNUM) AS QTY,SUM(A.FTAMOUNT)AS AMOUNT   FROM DBO.SAL_INVOICEA A      INNER JOIN SAL_SALEORDEA D ON A.FSALEORDENO=D.FSALEORDENO LEFT JOIN BSC_STOREHOUSE B ON A.SHOPNO = B. FCODE "
                If B = True Then
                    SQL += "  WHERE CONVERT(VARCHAR(10),FDATE,120) BETWEEN '" & SDate & "' AND '" & EDate & "' "
                    If Me.CBStatus.Text.ToString.Trim <> "" Then
                        SQL += " AND a.BillStatusid = '" & Status & "' "
                    End If
                    If Me.CBType.Text.ToString.Trim <> "" Then
                        SQL += " AND A.FTYPE = '" & Type & "' "
                    End If
                Else
                    SQL += " WHERE 1<>1 "
                End If
                SQL += "  GROUP BY A.FDATE  ORDER BY A.FDATE  "
                DT = Exec(SQL, E_ExecType.DataTable)
                Me.DataGridView1.DataSource = DT
                BindDataGridViewCaption(Me.DataGridView1, Me.ReturnCaption(2))
            End If
            If Me.RBOperater.Checked = True Then
                SQL = " SELECT FCNAME AS NAME ,COUNT(FNUM) AS QTY,SUM(A.FTAMOUNT)AS AMOUNT   FROM DBO.SAL_INVOICEA A      INNER JOIN SAL_SALEORDEA D ON A.FSALEORDENO=D.FSALEORDENO LEFT JOIN BSC_EMPLOYEE B ON A.BULIDBY = B.FCODE "
                If B = True Then
                    SQL += "  WHERE CONVERT(VARCHAR(10),FDATE,120) BETWEEN '" & SDate & "' AND '" & EDate & "' "
                    If Me.CBStatus.Text.ToString.Trim <> "" Then
                        SQL += " AND a.BillStatusid = '" & Status & "' "
                    End If
                    If Me.CBType.Text.ToString.Trim <> "" Then
                        SQL += " AND A.FTYPE = '" & Type & "' "
                    End If
                Else
                    SQL += " WHERE 1<>1 "
                End If
                SQL += "  GROUP BY FCNAME ORDER BY FCNAME  "
                DT = Exec(SQL, E_ExecType.DataTable)
                Me.DataGridView1.DataSource = DT
                BindDataGridViewCaption(Me.DataGridView1, Me.ReturnCaption(3))
            End If

            ReportTxt = "【發票日期從：" & SDate & " 到 " & EDate & ""
            If Me.CBStatus.Text.ToString.Trim <> "" Then
                ReportTxt += "　發票狀態：" & Me.CBStatus.Text.ToString.Trim & ""
            End If
            If Me.CBType.Text.ToString.Trim <> "" Then
                ReportTxt += "　發票類型：" & Me.CBType.Text.ToString.Trim & ""
            End If
            ReportTxt += "】"
            If B = True Then
                If DT.Rows.Count <= 0 Then
                    MessageBox.Show("沒有查詢到數據 ", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub
    Private Function ReturnCaption(ByVal index As Integer) As Hashtable
        Dim Hash As New Hashtable
        Select Case index
            Case 1
                Hash.Add("SHOP", "門店")
                Hash.Add("QTY", "發票數量")
                Hash.Add("AMOUNT", "發票金額")
            Case 2
                Hash.Add("FDATE", "發票日期")
                Hash.Add("QTY", "發票數量")
                Hash.Add("AMOUNT", "發票金額")
            Case 3
                Hash.Add("NAME", "操作人員")
                Hash.Add("QTY", "發票數量")
                Hash.Add("AMOUNT", "發票金額")
        End Select

        Return Hash
    End Function
    Private Sub Frm_ARSallinvoiceCountReports_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LodStatus()
        LodType()
        Me.RBShop.Checked = True
        SelectData(False)
    End Sub

    Private Sub DataGridView1_RowPostPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
        Try
            Dim DGView As DataGridView = DirectCast(sender, DataGridView)
            Dim SBrush As New SolidBrush(DGView.RowHeadersDefaultCellStyle.ForeColor)
            e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, System.Globalization.CultureInfo.CurrentCulture), e.InheritedRowStyle.Font, SBrush, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4)
        Catch ex As Exception
            MessageBox.Show("DataGridView1_RowPostPaint:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrint.Click
        Try
            If Me.DataGridView1.RowCount <= 0 Then
                MsgBox("沒有數據可以利列印", MsgBoxStyle.OkOnly, Msg.CompanyName)
                Exit Sub
            End If
            Dim DTable As DataTable = CType(Me.DataGridView1.DataSource, DataTable)
            If DTable.Rows.Count <= 0 Then
                Exit Sub
            End If
            If Me.RBShop.Checked = True Then
                App.SetPrint(Me.DataGridView1, "AR", "INVCountShop.rpt", "ReportTxt", ReportTxt)
            End If
            If Me.RBDate.Checked = True Then
                App.SetPrint(Me.DataGridView1, "AR", "INVCountDate.rpt", "ReportTxt", ReportTxt)
            End If
            If Me.RBOperater.Checked = True Then
                App.SetPrint(Me.DataGridView1, "AR", "INVCountOperater.rpt", "ReportTxt", ReportTxt)
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub
End Class
